/*=============================================================================
#    The element-comm is a tentative program used to test our ideas.           
#                       Copyright (C)  bgao                                    
#
#     This program is free software; you can redistribute it and/or            
#      modify it under the terms of the GNU General Public License             
#     as published by the Free Software Foundation; either version 2           
#         of the License, or (at your option) any later version.               
#
#     This program is distributed in the hope that it will be useful,          
#    but WITHOUT ANY WARRANTY; without even the implied warranty of           
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            
#             GNU General Public License for more details.                    
#
#    You should have received a copy of the GNU General Public License        
#     along with this program; if not, write to the Free Software             
#      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,             
#                       MA  02110-1301, USA.                                  
#==============================================================================
# $Id$ 
#
# $Author$ 
#
# $Revision$ 
#
# $Date$ 
#
# Description: 
#
=============================================================================*/
#include "ARM/arm-common.h"
#include "rtos-wsn.h"
#include "led.h"


extern void spi_init(void);
extern void usart0_init(void);
extern void cc2420_io_init(void);

static uint32 wsn_task_time;

void platform_wsn_init(void)
{
	spi_init();
    usart0_init();
	led_io_init();
	cc2420_io_init();
}

void  isr_timer3_comp(void)
{
    ClearPending(BIT_TIMER3);	
	wsn_LeaveCritical();		
    wsn_task_time += 320;
	wsn_timer_tick();
}


void wsn_start_timer(void)
{
	 uint32 tcon_value;
	 
    /* Init system timer */
    rTCFG0 = 0x000f0f; 	//Dead zone=0,Prescaler1=15(0x0f),Prescaler0=15(0x0f)
    rTCFG1 = 0x001233; 	//All interrupt,Mux4=1/2,Mux3=1/4,Mux2=1/8,Mux1=1/16,Mux0=1/16
	
   
    /* Start timer 3 as rtos system timer */
    tcon_value = rTCON & ~0xF0000;
    rTCNTB3 = 50000000 / (16 * 4) /3125; // 3125 ticks per second
    rTCON   = tcon_value | 0xA0000;	// manual update
    rTCON   = tcon_value | 0x90000;	// run

	/* Enable INT_TIMER3 */
    rINTMSK &= ~(BIT_TIMER3); 

}


/*****************************************************************************
 *
 * Timer for task sched.
 *
 *****************************************************************************
 */

uint32 Wsn_TaskSchedTime(void)
{
    return wsn_task_time;
}


void wsn_EnterCritical(void)
{	
	int tmp;
	__asm
	{
		MRS tmp, CPSR
		ORR tmp, tmp, #0xC0
		MSR CPSR_c, tmp
	}
}


void wsn_LeaveCritical(void)
{
	int tmp;
	__asm
	{
		MRS tmp, CPSR
		BIC tmp, tmp, #0xC0
		MSR CPSR_c, tmp
	}
}


void wsn_system_sleep(void)
{
	uint32 i;
	/*system Goto sleep ...*/
	WSN_debug(0x02cf,0);
	wsn_LeaveCritical();
	for(i=0;i<1000000;i++)
		;
}

